package com.example.mvcdemo.controller;

import com.example.mvcdemo.model.MessageInfo;
import com.example.mvcdemo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/4
 * Time: 18:57
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageService.queryMessage();
    }

    // @RequestBody 表示接收 json 数据
    @RequestMapping(value = "/publish", produces = "application/json")
//    @RequestMapping(value = "/publish")
    public String publish(@RequestBody MessageInfo messageInfo) {
        if (StringUtils.hasLength(messageInfo.getFrom())
                && StringUtils.hasLength(messageInfo.getTo())
                && StringUtils.hasLength(messageInfo.getMessage())) {
            messageService.insertMessage(messageInfo);
            return "{\"ok\":1}";
        }
        return "{\"ok\":0}";
    }
}
